package com.android.juguotang.fragment.group;


import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.juguotang.R;
import com.android.juguotang.activity.GoodsDetailActivity;
import com.android.juguotang.activity.jgtGoodsDetailActivity;
import com.android.juguotang.adapt.MainlistAdapt;
import com.android.juguotang.fragment.BaseLazyFragment;
import com.android.juguotang.model.group.MainList;
import com.android.juguotang.model.group.MainListCallback;
import com.android.juguotang.model.group.MainThematicActivity_Insert;
import com.android.juguotang.utils.ConstantsUrl;
import com.android.juguotang.utils.HttpUtils;
import com.android.juguotang.utils.MyLog;
import com.android.juguotang.utils.ObjectUtils;
import com.android.juguotang.view.recycleview.CustomLoadMoreView;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;


public class Fragment_Pintuan extends BaseLazyFragment implements BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.listView)
    RecyclerView mRecyclerView;
    private static String record;
    private View view;
    private FragmentActivity mcontext;
    private ImageView sort_list_listorder_iv;
    private ImageView sort_list_sales_iv;
    private ImageView sort_list_price_iv;
    private boolean is_order_asc = true;
    private boolean is_sales_asc = false;
    private boolean is_price_asc = false;
    private int page = 1;
    private AdaptList adapter;
    private boolean networkConnected;
    private String sort_style = "listorder";
    private String mParam2;
    private static final String ARG_PARAM2 = "param2";
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
//            is_ocsort = getArguments().getBoolean(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
//            mParam3 = getArguments().getString(ARG_PARAM3);
        }
    }
    public static Fragment_Pintuan newInstance(String mParam2 ) {
        Fragment_Pintuan fragment = new Fragment_Pintuan();
        Bundle args = new Bundle();

        record = mParam2;

        fragment.setArguments(args);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_pintuan, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        initUi(view);
        return view;
    }

    @Override
    protected void initPrepare() {

    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return null;
    }

    @Override
    public void onLoadMoreRequested() {

    }
    private void initUi(View inflate) {
        adapter = new AdaptList(mcontext, null, "0");
        // adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.setOnLoadMoreListener(this, mRecyclerView);
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        mRecyclerView.setAdapter(adapter);
//        mRecyclerView.GetToTop((ImageView) rootView.findViewById(R.id.top), 0);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
               // ObjectUtils.ToDetailActivity(mcontext, 1, adapter.getItem(position).getId(), "");
                Intent intent = new Intent(mcontext, GoodsDetailActivity.class);
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_DETAIL_SHOW, 1);
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_DETAIL_ID, adapter.getItem(position).getId());
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_DETAIL_COMMON_ID, "");
                mcontext.startActivity(intent);


            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public boolean onItemChildClick(BaseQuickAdapter adapter1, View view, int position) {
                switch (view.getId()) {
                    case R.id.main_list_item_go_kai:
                        ObjectUtils.ToDetailActivity(mcontext, 1, adapter.getItem(position).getId(), "");
                        break;
                }
                return false;
            }
        });
        LinearLayout sort_list_listorder = (LinearLayout) inflate.findViewById(R.id.sort_list_listorder);
        LinearLayout sort_list_sales = (LinearLayout) inflate.findViewById(R.id.sort_list_sales);
        LinearLayout sort_list_price = (LinearLayout) inflate.findViewById(R.id.sort_list_price);
        sort_list_listorder_iv = (ImageView) inflate.findViewById(R.id.sort_list_listorder_iv);
        sort_list_sales_iv = (ImageView) inflate.findViewById(R.id.sort_list_sales_iv);
        sort_list_price_iv = (ImageView) inflate.findViewById(R.id.sort_list_price_iv);
        sort_list_listorder_iv.setImageResource(R.mipmap.arrow_common);
        sort_list_sales_iv.setImageResource(R.mipmap.arrow_common);
        sort_list_price_iv.setImageResource(R.mipmap.arrow_common);
        sort_list_listorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!is_order_asc) {
                    is_order_asc = true;
                    sort_list_listorder_iv.setImageResource(R.mipmap.arrow_up);
                } else {
                    is_order_asc = false;
                    sort_list_listorder_iv.setImageResource(R.mipmap.arrow_down);
                }
                page = 1;
                is_sales_asc = false;
                is_price_asc = false;
                sort_list_sales_iv.setImageResource(R.mipmap.arrow_common);
                sort_list_price_iv.setImageResource(R.mipmap.arrow_common);
                sort_style = "listorder";
                GetListDataGoods(page, "&q=" + record);
            }
        });
        sort_list_sales.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!is_sales_asc) {
                    is_sales_asc = true;
                    sort_list_sales_iv.setImageResource(R.mipmap.arrow_up);
                } else {
                    is_sales_asc = false;
                    sort_list_sales_iv.setImageResource(R.mipmap.arrow_down);
                }
                page = 1;
                is_order_asc = false;
                is_price_asc = false;
                sort_list_listorder_iv.setImageResource(R.mipmap.arrow_common);
                sort_list_price_iv.setImageResource(R.mipmap.arrow_common);
                sort_style = "sales";
                GetListDataGoods(page, "&q=" + record);
            }
        });
        sort_list_price.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!is_price_asc) {
                    is_price_asc = true;
                    sort_list_price_iv.setImageResource(R.mipmap.arrow_up);
                } else {
                    is_price_asc = false;
                    sort_list_price_iv.setImageResource(R.mipmap.arrow_down);
                }
                page = 1;
                is_sales_asc = false;
                is_order_asc = false;
                sort_list_sales_iv.setImageResource(R.mipmap.arrow_common);
                sort_list_listorder_iv.setImageResource(R.mipmap.arrow_common);
                sort_style = "price";
                GetListDataGoods(page, "&q=" + record);
            }
        });


        GetListDataGoods(page, "&q=" + record);
    }

    private class AdaptList extends BaseMultiItemQuickAdapter<MainList.MainListdata.Mainlistdata, BaseViewHolder> {
        private Context content;

        public AdaptList(Context context, ArrayList<MainList.MainListdata.Mainlistdata> data, String type) {
            super(data);
            this.content = context;
            addItemType(ConstantsUrl.THEMATICGOODSLISTItemTYPE, R.layout.item_list_main);
        }

        @Override
        protected void convert(BaseViewHolder holder, final MainList.MainListdata.Mainlistdata mainlistdata) {
            switch (holder.getItemViewType()) {
                case ConstantsUrl.THEMATICGOODSLISTItemTYPE:
                    ObjectUtils.photo2(content, mainlistdata.getImg_src(), (ImageView) holder.getView(R.id.main_list_item_image));
                    holder.setText(R.id.main_list_item_title, mainlistdata.getName());
                    holder.setText(R.id.main_list_item_money_after,"￥"+ mainlistdata.getTeam_price());
                    holder.setText(R.id.main_list_item_count, mainlistdata.getDiscount_price() + "折");
                    holder.setText(R.id.main_list_item_tuannumber, mainlistdata.getTeam_num() + "人团");
                    holder.setText(R.id.main_list_item_alltuannumber2, "￥"+mainlistdata.getPrice());
                    ((TextView) holder.getView(R.id.main_list_item_alltuannumber2)).getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
                    holder.getView(R.id.main_list_item_go_kai).setVisibility(View.VISIBLE);

                    String sellNum = mainlistdata.getTeam_people_num();
                    if (!TextUtils.isEmpty(sellNum) && !sellNum.equals("0")) {
                        holder.getView(R.id.item_list_sort_lottory_time).setVisibility(View.VISIBLE);
                        holder.setText(R.id.item_list_sort_lottory_time, "已团" + sellNum + "件");
                         }
//                    ((TextView) holder.getView(R.id.item_grid_detail_like_number_before)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
//                    ObjectUtils.photo2(content, mainlistdata.getImg_src(), (ImageView) holder.getView(R.id.item_grid_detail_like_img));
//                    holder.getView(R.id.item_grid_open).setVisibility(View.GONE);
//                    holder.getView(R.id.item_grid_detail_like_number_before).setVisibility(View.GONE);
//                    holder.setText(R.id.item_grid_detail_like_title, mainlistdata.getName());
//                    holder.setText(R.id.item_grid_detail_like_number_after, mainlistdata.getTeam_price());
//                    holder.setText(R.id.item_grid_detail_like_number_before, mainlistdata.getPrice());
//                    String sellNum = mainlistdata.getTeam_people_num();
//                    if (!TextUtils.isEmpty(sellNum) && !sellNum.equals("0")) {
//                        holder.setText(R.id.item_grid_detail_like_tuanshu, "已团" + sellNum + "件");
//                    }
//                    holder.getView(R.id.iv_addtoCart).setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            addToCart(mainlistdata.getId());
//                        }
//                    });
                    break;
            }
        }
    }
    private ArrayList<MainList.MainListdata.Mainlistdata> listdata = new ArrayList<>();
    private ArrayList<MainList.MainListdata.Mainlistdata> listdatasearch_goods = new ArrayList<>();
    private void GetListDataGoods(final int page, String type) {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.MAIN_LISTDATA + "?page=" + page +"&typeid=1";
//            progressLoading.setVisibility(View.VISIBLE);
            MyLog.e("inest","address ="+address);
            HashMap<String, String> map = new HashMap<>();

            MyLog.e("adrreesss111", address);
            switch (sort_style) {
                case "listorder":
                    map.put("order", "listorder");
                    break;
                case "sales":
                    map.put("order", "sales");
                    break;
                case "price":
                    map.put("order", "price");
                    break;
                default:
                    break;
            }
            if (is_order_asc || is_price_asc || is_sales_asc) {
                map.put("sort", "asc");
            } else {
                map.put("sort", "desc");
            }
            HttpUtils.get(address, map, new MainListCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
//                    mRefreshLayout.setRefreshing(false);
//                    progressLoading.setVisibility(View.GONE);
//                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(MainList response, int id) {
//                    progressLoading.setVisibility(View.GONE);
//                    mRefreshLayout.setRefreshing(false);
                    if (response == null) {
//                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
//                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {

                        adapter.setNewData(response.getData().getList());
                        adapter.loadMoreEnd();
                    } else {
                        if (page == 1) {
//                            noData.setVisibility(View.VISIBLE);
                        } else {
                            adapter.loadMoreFail();
                            ObjectUtils.toast(mcontext, response.getMsg());
                        }
                    }
                }
            });
        } else {
            if (page == 1) {
//                ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            }
            ObjectUtils.toast(mcontext, "网络未连接");
            adapter.loadMoreFail();
        }
    }




}
